package cn.bjit.controller;


import cn.bjit.config.AlipayConfig;
import cn.bjit.config.PayUrlConfig;
import cn.bjit.constant.CacheKey;
import cn.bjit.enums.BizCodeEnum;
import cn.bjit.enums.ClientTypeEnum;
import cn.bjit.enums.ProductOrderPayTypeEnum;
import cn.bjit.interceptor.LoginInterceptor;
import cn.bjit.model.LoginUser;
import cn.bjit.request.ConfirmOrderRequest;
import cn.bjit.request.RepayOrderRequest;
import cn.bjit.service.ProductOrderService;
import cn.bjit.util.CommonUtil;
import cn.bjit.util.JsonData;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api("订单模块")
@Slf4j
@RestController
@RequestMapping("/api/order/v1")
public class ProductOrderController {
    @Resource
    private ProductOrderService orderService;
    @Autowired
    private PayUrlConfig payUrlConfig;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("提交订单")
    @PostMapping("confirm")
    public void confirmOrder(@ApiParam("订单对象") @RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response) {
        //下单
        JsonData jsonData = orderService.confirmOrder(orderRequest);
        if (jsonData.getCode() == 0) { //创建订单成功
            String payType = orderRequest.getPayType();//获取支付方式
            String clientType = orderRequest.getClientType(); //获取终端类型

            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALIPAY.name())) { //支付宝
                log.info("创建支付宝订单完成：{}", orderRequest.toString());
                if (clientType.equalsIgnoreCase(ClientTypeEnum.H5.name())) { //网页端
                    writeData(response, jsonData);
                } else if (clientType.equalsIgnoreCase(ClientTypeEnum.APP.name())) { //app端
                    // APP SDK支付 TODO
                }
            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT.name())) { //微信
                // TODO 微信支付
            }
        } else {
            log.error("创建订单失败{}", jsonData.toString());
            CommonUtil.sendJsonMessage(response, jsonData);
        }
    }

    private void writeData(HttpServletResponse response, JsonData jsonData) {
        try {
            response.setContentType("text/html;charset=UTF8");
            response.getWriter().write(jsonData.getData().toString());
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            log.error("写出Html异常：{}", e);
        }
    }

    /**
     * 查询订单状态
     *
     * @param outTradeNo
     * @return
     */
    @ApiOperation("查询订单状态")
    @GetMapping("query_state")
    public JsonData queryProductOrderState(@ApiParam("订单号") @RequestParam("out_trade_no") String outTradeNo) {
        String state = orderService.queryProductOrderState(outTradeNo);
        return StringUtils.isBlank(state) ? JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST) : JsonData.buildSuccess(state);
    }


    /**
     * 测试支付方法
     */
    @GetMapping("test_pay")
    public void testAlipay(HttpServletResponse response) throws AlipayApiException, IOException {

        HashMap<String, String> content = new HashMap<>();
        //商户订单号,64个字符以内、可包含字母、数字、下划线；需保证在商户端不重复
        String no = UUID.randomUUID().toString();

        log.info("订单号:{}", no);
        content.put("out_trade_no", no);

        content.put("product_code", "FAST_INSTANT_TRADE_PAY");

        //订单总金额，单位为元，精确到小数点后两位
        content.put("total_amount", String.valueOf("111.99"));

        //商品标题/交易标题/订单标题/订单关键字等。 注意：不可使用特殊字符，如 /，=，&amp; 等。
        content.put("subject", "html");

        //商品描述，可空
        content.put("body", "html");

        // 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
        content.put("timeout_express", "5m");


        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setBizContent(JSON.toJSONString(content));
        request.setNotifyUrl(payUrlConfig.getAlipayCallbackUrl());
        request.setReturnUrl(payUrlConfig.getAlipaySuccessReturnUrl());

        AlipayTradeWapPayResponse alipayResponse = AlipayConfig.getInstance().pageExecute(request);

        if (alipayResponse.isSuccess()) {
            System.out.println("调用成功");

            String form = alipayResponse.getBody();

            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(form);
            response.getWriter().flush();
            response.getWriter().close();

        } else {
            System.out.println("调用失败");
        }

    }


    /**
     * 分页查询我的订单列表
     *
     * @param page
     * @param size
     * @param state
     * @return
     */
    @ApiOperation("分页查询我的订单列表")
    @GetMapping("page")
    public JsonData pagePOrderList(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "订单状态") @RequestParam(value = "state", required = false) String state
    ) {
        Map<String, Object> pageResult = orderService.page(page, size, state);

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("重新支付订单")
    @PostMapping("repay")
    public void repay(@ApiParam("订单对象") @RequestBody RepayOrderRequest repayOrderRequest, HttpServletResponse response) {
        JsonData jsonData = orderService.repay(repayOrderRequest);
        if (jsonData.getCode() == 0) {
            String client = repayOrderRequest.getClientType();  //H5
            String payType = repayOrderRequest.getPayType(); //alipay
            //如果是支付宝网页支付，都是跳转网页，APP除外
            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALIPAY.name())) {
                log.info("重新支付订单成功:{}", repayOrderRequest.toString());
                if (client.equalsIgnoreCase(ClientTypeEnum.H5.name())) {
                    writeData(response, jsonData);

                } else if (client.equalsIgnoreCase(ClientTypeEnum.APP.name())) {
                    //APP SDK支付  TODO
                }
            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT.name())) {
                //微信支付 TODO
            }
        } else {
            log.error("重新支付订单失败{}", jsonData.toString());
            CommonUtil.sendJsonMessage(response, jsonData);
        }
    }

    @ApiOperation("获取提交订单令牌")
    @GetMapping("get_token")
    public JsonData getOrderToken() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        String key = String.format(CacheKey.SUBMIT_ORDER_TOKEN_KEY, loginUser.getId());
        String token = CommonUtil.getStringNumRandom(32); //生成一个token32位
        redisTemplate.opsForValue().set(key, token, 30, TimeUnit.MINUTES);  //30分钟过期
        return JsonData.buildSuccess(token);
    }

}